﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using UnityEngine;

    [StructLayout(LayoutKind.Sequential)]
    internal sealed class WebView : ScriptableObject
    {
        [SerializeField]
        private MonoReloadableIntPtr webViewWrapper;
        [CompilerGenerated]
        private static Dictionary<string, int> <>f__switch$map18;
        [CompilerGenerated]
        private static Dictionary<string, int> <>f__switch$map19;
        public WebView(int width, int height, bool showResizeHandle)
        {
            this.InitWebView(width, height, showResizeHandle);
        }

        public WebView(int width, int height, string url)
        {
            this.InitWebView(width, height, false);
            this.LoadURL(url);
        }

        public void OnDestroy()
        {
            this.DestroyWebView();
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern void DestroyWebView();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void InitWebView(int width, int height, bool showResizeHandle);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void LoadURL(string url);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void LoadFile(string path);
        internal Texture2D texture { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern bool IntPtrIsNull();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void SetDelegateObject(ScriptableObject value);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void SetWebkitControlsGlobalLocation(int x, int y);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void Resize(int width, int height);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void InjectMouseDown(int x, int y, int button, int clickCount);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void Cut();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void Copy();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void Paste();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void SelectAll();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void InjectMouseUp(int x, int y, int button, int clickCount);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void InjectMouseMove(int x, int y);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void InjectMouseDrag(int x, int y, int button);
        internal void InjectScrollWheel(int x, int y, Vector2 delta)
        {
            INTERNAL_CALL_InjectScrollWheel(this, x, y, ref delta);
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private static extern void INTERNAL_CALL_InjectScrollWheel(WebView self, int x, int y, ref Vector2 delta);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern void InjectKeyboardEvent(Event e);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void Focus();
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void UnFocus();
        internal bool InjectEvent(Event e, Rect position)
        {
            switch (e.type)
            {
                case EventType.MouseDown:
                    this.InjectMouseDown((int) (e.mousePosition.x - position.x), (int) (e.mousePosition.y - position.y), (((Application.platform != RuntimePlatform.OSXEditor) || !e.control) || (e.button != 0)) ? e.button : 1, e.clickCount);
                    break;

                case EventType.MouseUp:
                    this.InjectMouseUp((int) (e.mousePosition.x - position.x), (int) (e.mousePosition.y - position.y), (((Application.platform != RuntimePlatform.OSXEditor) || !e.control) || (e.button != 0)) ? e.button : 1, e.clickCount);
                    break;

                case EventType.MouseMove:
                    this.InjectMouseMove((int) (e.mousePosition.x - position.x), (int) (e.mousePosition.y - position.y));
                    break;

                case EventType.MouseDrag:
                    this.InjectMouseDrag((int) (e.mousePosition.x - position.x), (int) (e.mousePosition.y - position.y), e.button);
                    break;

                case EventType.KeyDown:
                case EventType.KeyUp:
                    this.InjectKeyboardEvent(e);
                    break;

                case EventType.ScrollWheel:
                    this.InjectScrollWheel((int) (e.mousePosition.x - position.x), (int) (e.mousePosition.y - position.y), e.delta);
                    break;

                default:
                    return false;
            }
            if (Event.current == null)
            {
                GUIUtility.ExitGUI();
            }
            return true;
        }

        public WebScriptObject windowScriptObject { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }
        public bool needsRepaint { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }
        public void DoGUI(Rect position)
        {
            int num = GUIUtility.GetControlID(this.GetHashCode(), FocusType.Native, position);
            EventType type = Event.current.type;
            switch (type)
            {
                case EventType.MouseDown:
                {
                    Vector2 vector = GUIUtility.GUIToScreenPoint(new Vector2(position.x, position.y));
                    this.SetWebkitControlsGlobalLocation((int) vector.x, (int) vector.y);
                    if (position.Contains(Event.current.mousePosition))
                    {
                        GUIUtility.hotControl = num;
                    }
                    break;
                }
                case EventType.MouseUp:
                    if (GUIUtility.hotControl == num)
                    {
                        GUIUtility.hotControl = 0;
                    }
                    break;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl == num)
                    {
                        Event.current.Use();
                    }
                    break;

                case EventType.Repaint:
                    GUI.DrawTexture(position, this.texture);
                    return;

                case EventType.Layout:
                    this.Resize((int) position.width, (int) position.height);
                    return;

                default:
                    string commandName;
                    Dictionary<string, int> dictionary;
                    int num2;
                    if (type == EventType.ValidateCommand)
                    {
                        commandName = Event.current.commandName;
                        if (commandName != null)
                        {
                            if (<>f__switch$map18 == null)
                            {
                                dictionary = new Dictionary<string, int>(4);
                                dictionary.Add("Cut", 0);
                                dictionary.Add("Copy", 0);
                                dictionary.Add("Paste", 0);
                                dictionary.Add("SelectAll", 0);
                                <>f__switch$map18 = dictionary;
                            }
                            if (<>f__switch$map18.TryGetValue(commandName, out num2) && (num2 == 0))
                            {
                                Event.current.Use();
                            }
                        }
                        return;
                    }
                    else if (type == EventType.ExecuteCommand)
                    {
                        commandName = Event.current.commandName;
                        if (commandName != null)
                        {
                            if (<>f__switch$map19 == null)
                            {
                                dictionary = new Dictionary<string, int>(4);
                                dictionary.Add("Cut", 0);
                                dictionary.Add("Copy", 1);
                                dictionary.Add("Paste", 2);
                                dictionary.Add("SelectAll", 3);
                                <>f__switch$map19 = dictionary;
                            }
                            if (<>f__switch$map19.TryGetValue(commandName, out num2))
                            {
                                switch (num2)
                                {
                                    case 0:
                                        this.Cut();
                                        break;

                                    case 1:
                                        this.Copy();
                                        break;

                                    case 2:
                                        this.Paste();
                                        break;

                                    case 3:
                                        this.SelectAll();
                                        break;
                                }
                            }
                        }
                        return;
                    }
                    break;
            }
            if (this.InjectEvent(Event.current, position))
            {
                Event.current.Use();
            }
            else if (GUIUtility.hotControl == num)
            {
                GUIUtility.hotControl = 0;
            }
        }

        public void DoGUILayout(params GUILayoutOption[] options)
        {
            Rect position = GUILayoutUtility.GetRect(new GUIContent(), EditorStyles.textField, options);
            if ((Event.current.type != EventType.Layout) && (Event.current.type != EventType.MouseUp))
            {
                this.Resize((int) position.width, (int) position.height);
                this.DoGUI(position);
            }
        }

        public void Back()
        {
            this.windowScriptObject.EvalJavaScript("history.back();");
        }

        public void Forward()
        {
            this.windowScriptObject.EvalJavaScript("history.forward();");
        }

        public void Reload()
        {
            this.windowScriptObject.EvalJavaScript("document.location.reload(true);");
        }

        public static implicit operator bool(WebView exists)
        {
            return ((exists != null) && !exists.IntPtrIsNull());
        }
    }
}

